home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / qlib205.zip / QLIB.ZIP / TEST / TIMER.ASM < prev    next >
Assembly Source File  |  1997-07-12  |  4KB  |  227 lines

  1. ;this code is to test 2 tests of code to see which is faster
  2. ;place code in timer.inc, compile and run it...
  3.  
  4. ;you must run this under DOS, windoze does not allow to tamper with the PIT
  5.  
  6. ;NOTE : this program sets timer#0 to as fast as possible so this program
  7. ;       traps IRQ1 and does not let it go to DOS, so your time
  8. ;       will simply freeze when you use this program (instead of zooming
  9. ;       into the next millinium)
  10.  
  11. ;NEW : Now takes advantage of Pentium cycle clock (opcode 0fh,31h)
  12.  
  13. ; timer.inc holds the code to be timed it also 
  14. ;   holds some of my findings
  15. ; results are VERY accurate (unless you run this under an program that does
  16. ;   not allow this to alter the PIT)
  17.  
  18. include qlib.inc
  19. include stdio.inc
  20. include conio.inc
  21. include alloc.inc
  22.  
  23. .data
  24.  
  25. spdhi equ 000h
  26. spdlo equ 010h
  27.  
  28. loops equ 100000
  29.   ;no of times to do c1,c2 each time
  30.   ;change this to a comfortable speed for your CPU
  31.   ;I have a P90
  32.  
  33. align 4  ;ok, I think I went align crazy here...
  34. s1 dd 0
  35. s2 dd 0
  36.  
  37. oldIRQ1 df ?
  38. align 4
  39. counter dd ?
  40. counter2 dd ?
  41. pre dw 100  ;it does this many times before it starts average mode
  42.  
  43. om dw ? ;old IRQ masks
  44.  
  45. .code
  46. align 4
  47. include timer.inc
  48.  
  49. align 4
  50. irq1 proc
  51.   push ds
  52.   push ax
  53.   mov ds,cs:seldata
  54.   inc counter
  55.   mov al,20h
  56.   out 20h,al
  57.   pop ax
  58.   pop ds
  59.   iretd
  60. irq1 endp
  61.  
  62. align 4
  63. main proc
  64.   in al,021h
  65.   shl ax,8
  66.   in al,0a1h
  67.   mov om,ax
  68.  
  69.   .if _cpu < 5
  70.     callp getint,8
  71.     mov wptr[oldIRQ1+4],ax
  72.     mov dptr[oldIRQ1],edx
  73.  
  74.     mov cx,cs
  75.     callp setint,8,cx,offset irq1
  76.  
  77.     mov al,0ffh-3
  78.     out 021h,al   ;enable only the timer and kbd
  79.     mov al,0ffh
  80.     out 0a1h,al 
  81.  
  82.     ;setup a fast TIMER
  83.     cli
  84.     mov al,00110110b
  85.     out 43h,al
  86.     mov al,spdlo
  87.     out 40h,al
  88.     mov al,spdhi       ;1191180 = freq of timer
  89.     out 40h,al  ;really fast
  90.     sti
  91.   .else   ;Pentium
  92.     mov al,0ffh-2
  93.     out 021h,al   ;enable only the kbd
  94.     mov al,0ffh
  95.     out 0a1h,al 
  96.   .endif
  97.  
  98.   call clrscr
  99.   callp printf,"Code #1 =\n"
  100.   callp printf,"Code #2 =\n"
  101.   callp printf,"Counter =\n"
  102.  
  103. align 4
  104. top:
  105.   .if _cpu >= 5
  106.     db 0fh,31h  ;EDX:EAX = cycle clock
  107.     mov counter,eax
  108.     mov counter2,edx
  109.   .endif
  110.   callp gotoxy,10,3
  111.   callp printf,"%10u",counter
  112.   mov ecx,loops
  113. align 4
  114.   .if _cpu >= 5
  115.     db 0fh,31h  ;EAX:EDX = cycle clock
  116.     mov counter,eax
  117.     mov counter2,edx
  118.   .endif
  119.   mov edx,counter
  120. align 4
  121. _c1:
  122.   push ecx
  123.   push edx
  124.   c1
  125.   pop edx
  126.   pop ecx
  127.   dec ecx
  128.   jnz _c1
  129.   .if _cpu >= 5
  130.     push edx
  131.     db 0fh,31h  ;EDX:EAX = cycle clock
  132.     mov counter,eax
  133.     mov eax,edx
  134.     pop edx
  135.     .if eax != counter2
  136.       jmp _i1  ;ignore this last turn (counter flipped)
  137.     .endif
  138.   .endif
  139.   mov ecx,counter
  140.   .if edx>ecx
  141.     jmp _i1  ;counter fliped so just ignore this turn
  142.   .endif
  143.   sub ecx,edx
  144.   .if pre
  145.     mov s1,ecx
  146.   .else
  147.     add s1,ecx
  148.     shr s1,1
  149.   .endif
  150.   callp gotoxy,10,1
  151.   callp printf,"%10u",s1
  152. _i1:
  153.  
  154.   mov ecx,loops
  155. align 4
  156.   .if _cpu >= 5
  157.     db 0fh,31h  ;EDX:EAX = cycle clock
  158.     mov counter,eax
  159.     mov counter2,edx
  160.   .endif
  161.   mov edx,counter
  162. align 4
  163. _c2:
  164.   push ecx
  165.   push edx
  166.   c2
  167.   pop edx
  168.   pop ecx
  169.   dec ecx
  170.   jnz _c2
  171.   .if _cpu >= 5
  172.     push edx
  173.     db 0fh,31h  ;EDX:EAX = cycle clock
  174.     mov counter,eax
  175.     mov eax,edx
  176.     pop edx
  177.     .if eax != counter2
  178.       jmp _i2  ;counter flip
  179.     .endif
  180.   .endif
  181.   mov ecx,counter
  182.   .if edx>ecx
  183.     jmp _i2  ;counter fliped so just ignore this turn
  184.   .endif
  185.   sub ecx,edx
  186.   .if pre
  187.     mov s2,ecx
  188.     dec pre
  189.   .else
  190.     add s2,ecx
  191.     shr s2,1
  192.   .endif
  193.   callp gotoxy,10,2
  194.   callp printf,"%10u",s2
  195. _i2:
  196.   call kbhit
  197.   cmp al,0
  198.   jz top
  199.  
  200.   call getch
  201.  
  202.   .if _cpu < 5
  203.     cli
  204.     mov al,00110110b
  205.     out 43h,al
  206.     xor al,al
  207.     out 40h,al
  208.     out 40h,al  ;Default speed (18.2 / sec)
  209.     sti
  210.  
  211.     mov cx,wptr[oldIRQ1+4]
  212.     mov edx,dptr[oldIRQ1]
  213.     callp setint,8,cx,edx
  214.   .endif
  215.  
  216.   mov ax,om
  217.   out 0a1h,al
  218.   shr ax,8
  219.   out 021h,al
  220.  
  221.   callp gotoxy,1,4
  222.   ret
  223. main endp
  224.  
  225. end
  226.  
  227.